<?php
//dezend by http://www.yunlu99.com/ QQ:270656184
defined('BASEPATH') || exit('No direct script access allowed');
class Fill_eth_run extends MY_Controller
{
	public function __construct()
	{
		parent::__construct();
	}

	public function index()
	{
		set_time_limit(0);
		$this->load->library('bag');
		$coin = $this->d->find(array('table' => 'coin', 'where' => 'id=2'));
		$list = $this->bag->get_accounts($coin['host'], $coin['port']);

		foreach ($list as $v) {
			$balance = $this->bag->get_balance($coin['host'], $coin['port'], $v);
			$rs = array();
			$this->db->query('set autocommit=0');
			$this->db->query('lock tables t_fill_coin write, t_xnb write');
			$xnb = $this->d->find(array('table' => 'xnb', 'where' => 'coin_id=' . $coin['id'] . ' and address="' . $v . '"'));

			if ($xnb['address_balance'] < $balance) {
				$amount = num($balance - $xnb['address_balance']);
			}
			else {
				$this->db->query('rollback');
				$this->db->query('unlock tables');
				continue;
			}

			if ($amount <= 0) {
				$this->db->query('rollback');
				$this->db->query('unlock tables');
				continue;
			}

			$rs[] = $this->db->set('num', 'num+' . $amount, false)->set('address_balance', $balance)->where('id', $xnb['id'])->update('xnb');
			$d = array('user_id' => $xnb['user_id'], 'coin_id' => $coin['id'], 'num' => $amount, 'address' => $v, 'add_time' => time(), 'status' => 1);
			$rs[] = $this->db->insert('fill_coin', $d);

			if (correct($rs)) {
				$this->db->query('commit');
			}
			else {
				$this->db->query('rollback');
			}

			$this->db->query('unlock tables');
		}
	}
}

?>
